package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    RoleDao roleDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page);
    }

    @Override
    @Transactional
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);

        if(role.getId()!=null){
            if(permissionIds!=null && permissionIds.length>0){
                for (Integer permissionId : permissionIds) {
                    roleDao.setPermissions(role.getId(),permissionId);
                }
            }else {
                throw new RuntimeException("角色权限增加失败");
            }

            if(menuIds!=null && menuIds.length>0){
                for (Integer menuId : menuIds) {
                    roleDao.setMenus(role.getId(),menuId);
                }
            }else {
                throw new RuntimeException("角色菜单增加失败");
            }
        }
    }

    @Override
    @Transactional
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.edit(role);
        roleDao.delPermissions(role.getId());
        roleDao.delMenus(role.getId());
        if(role.getId()!=null){
            if(permissionIds!=null && permissionIds.length>0){
                for (Integer permissionId : permissionIds) {
                    roleDao.setPermissions(role.getId(),permissionId);
                }
            }else {
                throw new RuntimeException("角色权限更新失败");
            }

            if(menuIds!=null && menuIds.length>0){
                for (Integer menuId : menuIds) {
                    roleDao.setMenus(role.getId(),menuId);
                }
            }else {
                throw new RuntimeException("角色菜单更新失败");
            }
        }
    }

    @Override
    @Transactional
    public void delById(int roleId) {
        Long count = roleDao.findUserCountByRoleId(roleId);

        if(count == 0){
            roleDao.delPermissions(roleId);
            roleDao.delMenus(roleId);
            roleDao.delById(roleId);
        }else {
            throw new RuntimeException("有用户与该角色关联,不可删除!");
        }
    }

    @Override
    public Integer[] findPermissions(Integer roleId) {
        return roleDao.findPermissions(roleId);
    }

    @Override
    public Integer[] findMenus(Integer roleId) {
        return roleDao.findMenus(roleId);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

}
